package com.example.xm.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xm.myapplication.bean.PetInfo;
import com.example.xm.myapplication.utils.JsonParse;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.image.SmartImageView;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<PetInfo> piList;
    private ListView IvPet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IvPet = (ListView) findViewById(R.id.IvPets);
        downloadData();
        IvPet.setOnItemClickListener(new MyOnItemClickListener());
    }

    private void downloadData() {
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(getString(R.string.serverurt), new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String json = new String(bytes,"utf-8");
                    piList = JsonParse.getPetInfo(json);
                    if(piList == null) {
                        Toast.makeText(MainActivity.this,"解析失败",Toast.LENGTH_SHORT).show();
                    }else {
                        IvPet.setAdapter(new PetAdapeter());
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class PetAdapeter extends BaseAdapter{

        @Override
        public int getCount() {
            return piList.size();
        }

        @Override
        public Object getItem(int position) {
            return piList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private class ViewHolder{
            SmartImageView siv;
            TextView tvTitle;
            TextView tvContent;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                convertView= LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item,parent,false);
                holder = new ViewHolder();
                holder.siv = (SmartImageView)convertView.findViewById(R.id.sivIcon);
                holder.tvContent = (TextView)convertView.findViewById(R.id.tvContent);
                holder.tvTitle = (TextView)convertView.findViewById(R.id.tvTitle);
                convertView.setTag(holder);
            }else {
                holder =(ViewHolder) convertView.getTag();
            }
            PetInfo pi = piList.get(position);
            holder.siv.setImageUrl(pi.getIcon(),R.mipmap.ic_launcher,R.mipmap.ic_launcher);
            holder.tvTitle.setText(pi.getTitle());
            holder.tvContent.setText(pi.getContent());

            return convertView;
        }
    }

    private class MyOnItemClickListener implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            PetInfo pi = (PetInfo) parent.getItemAtPosition(position);
            Intent intent = new Intent(MainActivity.this,ShowActivity.class);
            intent.putExtra("obj",pi);
            startActivity(intent);
        }
    }

}
